import React from 'react';
import PropTypes from 'prop-types';

const Redirect = ({ href, url, runFun, style, children, ...otherProps }) => {
    if (!runFun) {
        console.warn('跳转没记录状态');
    }
    if (!url && !href) {
        // console.warn('跳转没链接地址' + children);
    }
    const link = href || url;
    const handleClick = () => {
        if (link) {
            runFun && runFun();
            WPT.redirect(link);
        }
    };
    const redirect = link ? { 'data-redirect': link } : {};
    const newProps = {
        ...otherProps,
        ...redirect,
    };

    return (
        // eslint-disable-next-line no-script-url
        <a href="#!" {...newProps} style={style} {...WPT.onTouchEnd(handleClick)}>
            {children}
        </a>
    );
};

Redirect.propTypes = {
    href: PropTypes.string,
    // 旧页面写法
    url: PropTypes.string,
    // 页面切换前调用
    runFun: PropTypes.func,
    style: PropTypes.object,
    children: PropTypes.node.isRequired,
};

Redirect.defaultProps = {
    href: '',
    url: '',
    style: {},
    runFun: () => {},
};

export default Redirect;
